<?php
date_default_timezone_set('Asia/Shanghai');//时区配置
$chesterClass = array();
$soap = new SoapServer(null,array('uri'=>"http://www.gsdai.com/api/bidServer.php",'chesterClass'=>$chesterClass,'encoding'=>'utf-8'));
$soap->setClass('chesterClass');
$soap->handle();



class chesterClass {
    
    /**
     * 登陆
     * @param string $username 登陆名
     * @param string $password 登陆密码
     * @return string 正确返回一个加密字符串/错误返回字符串Error
     */
    function login($username='', $password=''){
    	if($username=='wdzjuname' && $password == 'wdzjpwd'){
			return md5(md5($username.$password));
		}else{
			return "Error";
		}
    }
    
    /**
     * 获取当前正在招标的借款
     * @param string $token 登陆成功后获取到的加密字符串
     * @return 失败
     * 
     */
    function getNewBidInfo($token=''){
    	if(md5(md5('wdzjunamewdzjpwd'))!=$token){
    		return "";
    	}
    	include 'mymc.php';
		$cache = new Mymc();
		$key = 'api_bid_getNewBidInfo';
		$data = $cache->get_array($key); 
		
		if ($data==false){
			$data = array();
			
			include 'db.php'; //数据库连接
			
			$now = time();
			
			$sql = "select p1.*,p2.username,p3.name as use_type from ych_borrow as p1 left join ych_user as p2 on p1.user_id=p2.user_id left join ych_linkage as p3 on p1.fuse=p3.id where p1.fstatus=1 or(p1.fstatus=3 and p1.ftype=4 and p1.lz_endtime>UNIX_TIMESTAMP() and p1.account!=p1.account_yes )";
			//$sql = "select p1.*,p2.username,p3.name as use_type from ych_borrow as p1 left join ych_user as p2 on p1.user_id=p2.user_id left join ych_linkage as p3 on p1.fuse=p3.id where p1.fstatus=1";
			//$sql = "select p1.*,p2.username,p3.name as use_type from {borrow} as p1 left join {user} as p2 on p1.user_id=p2.user_id left join {linkage} as p3 on p1.use=p3.id where p1.status=1 or(p1.status=3 and p1.is_circulation=1 and p1.circulation_endtime>{$now} )";
			
			
			$list = self::db_fetch_arrays($sql,$conn);
			
			
			foreach($list as $key => $val){
				$type = 0;
				$RepaymentType = 3; //还款方式[1:到期还本息2.每月付息到期还本3.每月等额本息4.满额即还]
				if($val["ftype"]==5){
					$type = 1;//$tmptype[] = "秒";
					$RepaymentType = 4;
				}elseif($val["ftype"]==2){
					$type = 2;//$tmptype[] = "押";
				}elseif($val["ftype"]==3){
					$type = 3;//$tmptype[] = "净";
				}elseif($val["ftype"]==6){
					$type = 4;//$tmptype[] = "担";
				}elseif($val["ftype"]==4){
					$type = 6;//$tmptype[] = "流转标";
					$RepaymentType = 2;
				}elseif($val["ftype"]==1){
					$type = 5;//$tmptype[] = "信";
				}
				
				$string = "/static/data/avatar/".$val['user_id']."_avatar_middle.jpg";
				$pic = "http://www.gsdai.com".$string;
				
				$status = 0;
				if($val['fstatus']==1){
					$status = 1;//招标中
				}elseif($val['fstatus'] == 3 && $val['repayment_account'] != $val['repayment_yesaccount']){
					$status = 2;//待还中
				}elseif($val['fstatus'] == 3 && $val['repayment_account'] == $val['repayment_yesaccount']){
					$status = 3;//已完成
				}elseif(($val['verify_time'] + $val['valid_time']*24*3688)<time()){//p1.verify_time+p1.valid_time*24*60*60<".time();
					$status = 4;//逾期中
				}
				
				//投标人查询
				$sql2 = "select p1.id,p2.username,p1.money,p1.account,p1.add_time from ych_borrow_tender as p1 left join ych_user as p2 on p1.user_id=p2.user_id where p1.borrow_id=".$val['id'];
				$tenderUser = self::db_fetch_arrays($sql2,$conn);
				$interestRate = number_format($val["apr"]/100, 4, '.','');//年利率
				$tmpUser = array();
				foreach($tenderUser as $k => $v){
					$tmpUser[] = array(
						'Id' => $v['id'],//ID
						'SubscribeUserId' => urlencode($v['username']),//投标人ID
						'Money' => $v['money'],//投标金额
						'Amount' => $v['account'],//有效金额
						'AddDate' => date('YmdHis',$v['add_time']),//投标时间
						'Status' => $status,//状态0:无效投资1:投标中;2:待还款;3:已还款;4:逾期
						'Type' => 1,
						'InterestRate' => $interestRate
					);
				}
				
				
				
				$data[] = array(
					'ProjectId' => $val['id'],//项目主键
					'Title' => urlencode($val['title']),//借款标题
					'Amount' => $val['account'],//借款金额
					'Schedule' => number_format($val["account_yes"]/$val["account"], 4, '.',''),//完成进度
					'InterestRate' => $interestRate,//利率
					'TuandaiRate' => number_format($val['funds']/100, 4, '.',''),//奖励比例
					'Deadline' => $val["is_day"] == 1 ? $val["time_limit_day"] : $val["time_limit"],//借款期限
					'DeadLineUnit' => $val['is_day'] ? 1:0,//期限单位（1日0月）
					'Type'=> $type,//借款类型[1秒/2抵押/3净值/4担保/5信用/6流转标]
					'RepaymentType' => $RepaymentType,//还款方式[1:到期还本息2.每月付息到期还本3.每月等额本息4.满额即还]
					'TotalShares' => '',//总共多少份
					'CastedShares' => '',//已投多少份
					'Province' => urlencode('广东'),//省
					'City' => urlencode('深圳'),//市
					'UserName' => urlencode($val['username']),//发标人
					'UserAvatarUrl' => $pic,//发标人头像
					'AmountUsedDesc' => urlencode($val['use_type']),//用途
					'Subscribes' => $tmpUser,//投资人情报对象数据或LIST
				);
				
				
			}
			$cache->set_array($key,$data,600); 
		}
    	return json_encode($data);
	}
	
	/**
	 * 根据时间获取借款成功的借款
	 * @param string $token 登陆成功后获取到的加密字符串
	 * @param string $date  日期
	 */
	function getSuccessBidInfo($token='',$date=''){
		if(md5(md5('wdzjunamewdzjpwd'))!=$token){
    		return "";
    	}
    	include 'mymc.php';
		$cache = new Mymc();
		$key = 'api_bid_getSuccessBidInfo_'.$date;
		$data = $cache->get_array($key); 
		if ($data==false){
			$data = array();
			
			include 'db.php'; //数据库连接
			
			$date = $date ? $date : date('Y-m-d');
			$date = date('Y-m-d',strtotime($date));
			$today = date('Ymd',strtotime($date));;
			$date_start = strtotime($date);
			$date_end = strtotime($date." 23:59:59");
			//$sql = "select p1.*,p2.username,p3.name as use_type from {borrow} as p1 left join {user} as p2 on p1.user_id=p2.user_id left join {linkage} as p3 on p1.use=p3.id where (p1.status=3 and p1.repayment_time>={$date_start} and p1.repayment_time<={$date_end} and p1.is_circulation!=1) or(p1.status=3 and p1.is_circulation=1 and p1.id in(select DISTINCT borrow_id from dw_borrow_tender where addtime>={$date_start} and addtime<={$date_end}))";
			$sql = "select p1.*,p2.username,p3.name as use_type from ych_borrow as p1 left join ych_user as p2 on p1.user_id=p2.user_id left join ych_linkage as p3 on p1.fuse=p3.id where (p1.fstatus=3 and p1.repayment_time>={$date_start} and p1.repayment_time<={$date_end} and p1.ftype!=4) or(p1.fstatus=3 and p1.ftype=4 and p1.id in(select DISTINCT borrow_id from ych_borrow_tender where add_time>={$date_start} and add_time<={$date_end}))";
			
			$list = self::db_fetch_arrays($sql,$conn);
			foreach($list as $key => $val){
				$type = 0;
				$RepaymentType = 3; //还款方式[1:到期还本息2.每月付息到期还本3.每月等额本息4.满额即还]
				if($val["ftype"]==5){
					$type = 1;//$tmptype[] = "秒";
					$RepaymentType = 4;
				}elseif($val["ftype"]==2){
					$type = 2;//$tmptype[] = "押";
				}elseif($val["ftype"]==3){
					$type = 3;//$tmptype[] = "净";
				}elseif($val["ftype"]==6){
					$type = 4;//$tmptype[] = "担";
				}elseif($val["ftype"]==4){
					$type = 6;//$tmptype[] = "流转";
					$RepaymentType = 2;
				}elseif($val["ftype"]==1){
					$type = 5;//$tmptype[] = "信";
				}
				
				$string = "/static/data/avatar/".$val['user_id']."_avatar_middle.jpg";
				$pic = "http://www.gsdai.com".$string;
				
				$status = 0;
				if($val['fstatus']==1){
					$status = 1;//招标中
				}elseif($val['fstatus'] == 3 && $val['repayment_account'] != $val['repayment_yesaccount']){
					$status = 2;//待还中
				}elseif($val['fstatus'] == 3 && $val['repayment_account'] == $val['repayment_yesaccount']){
					$status = 3;//已完成
				}elseif(($val['verify_time'] + $val['valid_time']*24*3688)<time()){//p1.verify_time+p1.valid_time*24*60*60<".time();
					$status = 4;//逾期中
				}
				
				//投标人查询
				$sql2 = "select p1.id,p2.username,p1.money,p1.account,p1.add_time from ych_borrow_tender as p1 left join ych_user as p2 on p1.user_id=p2.user_id where p1.borrow_id=".$val['id'];
				if($val["ftype"]==4){
					$sql2 = "select p1.id,p2.username,p1.money,p1.account,p1.add_time from ych_borrow_tender as p1 left join ych_user as p2 on p1.user_id=p2.user_id where p1.borrow_id=".$val['id']." and p1.add_time>={$date_start} and p1.add_time<={$date_end} ";
				}
				
				$tenderUser = self::db_fetch_arrays($sql2,$conn);
				$interestRate = number_format($val["apr"]/100, 4, '.','');//年利率
				$tmpUser = array();
				$tmpAccount = 0;
				foreach($tenderUser as $k => $v){
					$tmpUser[] = array(
						'Id' => $v['id'],//ID
						'SubscribeUserId' => urlencode($v['username']),//urlencode(mb_convert_encoding($v['username'],"UTF-8","GBK")),//投标人ID
						'Money' => $v['money'],//投标金额
						'Amount' => $v['account'],//有效金额
						'AddDate' => date('YmdHis',$v['add_time']),//投标时间
						'Status' => $status,//状态1:正在申购2:申购失败3:回收中4:逾期中5:投资完成
						'Type' => 1,
						'InterestRate' => $interestRate
					);
					if($val["ftype"]==4){
						$tmpAccount = $tmpAccount + $v['account'];
					}
				}
				
				$revenue = 0;
				if($val["is_day"] == 1){
					$revenue = round($val['account'] * 0.015,2);
				}else{
					if($val["time_limit"] == 1){
						$revenue = round($val['account'] * 0.015,2);
					}elseif($val["time_limit"] == 2){
						$revenue = round($val['account'] * 0.02,2);
					}else{
						if($val["time_limit"]==1){
							$revenue = round($val['account'] * 0.015,2);
						}elseif($val["time_limit"]==2){
							$revenue = round($val['account'] * 0.02,2);
						}else{
							$t = ($val["time_limit"]-2)*0.002 + 0.02;
							$revenue = round($val['account'] * $t,2);
						}
					}
				}
				$data[] = array(
					'ProjectId' => $val["ftype"]==4 ? $val['id'].'_'.$today : $val['id'],//项目主键
					'Title' => urlencode($val['title']),//urlencode(mb_convert_encoding($val['name'],"UTF-8","GBK")),//借款标题
					'Amount' => $val["ftype"]==4 ? $tmpAccount : $val['account'],//借款金额   流转标有区别
					'Schedule' => number_format($val["account_yes"]/$val["account"], 4, '.',''),//完成进度
					'InterestRate' => $interestRate,//利率
					'TuandaiRate' => number_format($val['funds']/100, 4, '.',''),//奖励比例
					'Deadline' => $val["is_day"] == 1 ? $val["time_limit_day"] : $val["time_limit"],//借款期限
					'DeadLineUnit' => $val['is_day'] ? 1:0,//期限单位（1日0月）
					'Type'=> $type,//借款类型[1秒/2抵押/3净值/4担保/5信用/6流转标]
					'RepaymentType' => $RepaymentType,//还款方式[1:到期还本息2.每月付息到期还本3.每月等额本息4.满额即还]
					'TotalShares' => '',//总共多少份
					'CastedShares' => '',//已投多少份
					'Province' => urlencode('广东'),//urlencode(mb_convert_encoding('广东',"UTF-8","GBK")),//省
					'City' => urlencode('深圳'),//urlencode(mb_convert_encoding('深圳',"UTF-8","GBK")),//市
					'UserName' => $val['username'],//urlencode(mb_convert_encoding($val['username'],"UTF-8","GBK")),//发标人
					'UserAvatarUrl' => $pic,//发标人头像
					'AmountUsedDesc' => urlencode($val['use_type']),//urlencode(mb_convert_encoding($val['use_type'],"UTF-8","GBK")),//用途
					'Revenue' => $revenue,//管理费
					'Subscribes' => $tmpUser,//投资人情报对象数据或LIST
				);
			}
			
			$sql = "SELECT p1.yjb_id,p1.time_limit_day,SUM(p1.account) AS account,p1.apr,p2.user_id,
				p2.title,p2.account AS old_account,p2.account_yes,p2.fstatus,p3.username 
				FROM ych_yjb_tender AS p1 
				LEFT JOIN ych_yjb_borrow AS p2 ON p1.yjb_id=p2.id 
				LEFT JOIN ych_user as p3 ON p2.user_id=p3.user_id 
				where p1.add_time>={$date_start} and p1.add_time<={$date_end} 
				GROUP BY p1.yjb_id,p1.time_limit_day";
			$list = self::db_fetch_arrays($sql,$conn);
			foreach ($list as $val){
				$sql2 = "select p1.id,p2.username,p1.money,p1.account,p1.add_time from ych_yjb_tender as p1 left join ych_user as p2 on p1.user_id=p2.user_id where p1.yjb_id={$val['yjb_id']} and p1.time_limit_day={$val['time_limit_day']}";
				$tenderUser = self::db_fetch_arrays($sql2,$conn);
				$interestRate = number_format($val["apr"]/100, 4, '.','');//年利率
				$tmpUser = array();
				foreach($tenderUser as $k => $v){
					$tmpUser[] = array(
						'Id' => $v['id'],//ID
						'SubscribeUserId' => urlencode($v['username']),//urlencode(mb_convert_encoding($v['username'],"UTF-8","GBK")),//投标人ID
						'Money' => $v['money'],//投标金额
						'Amount' => $v['account'],//有效金额
						'AddDate' => date('YmdHis',$v['add_time']),//投标时间
						'Status' => $val['fstatus']<4?3:5,//状态1:正在申购2:申购失败3:回收中4:逾期中5:投资完成
						'Type' => 1,
						'InterestRate' => $interestRate
					);
				}
				$revenue = round($val['account'] * 0.015,2);
				
				$data[] = array(
					'ProjectId' => $val['yjb_id'].'_'.$val['time_limit_day'].'_'.$today,//项目主键
					'Title' => urlencode($val['title']),//urlencode(mb_convert_encoding($val['name'],"UTF-8","GBK")),//借款标题
					'Amount' => $val['account'],//借款金额   流转标有区别
					'Schedule' => number_format($val["account_yes"]/$val["old_account"], 4, '.',''),//完成进度
					'InterestRate' => $interestRate,//利率
					'TuandaiRate' => number_format($val['funds']/100, 4, '.',''),//奖励比例
					'Deadline' => $val["time_limit_day"],//借款期限
					'DeadLineUnit' => 1,//期限单位（1日0月）
					'Type'=> 4,//借款类型[1秒/2抵押/3净值/4担保/5信用/6流转标]
					'RepaymentType' => 1,//还款方式[1:到期还本息2.每月付息到期还本3.每月等额本息4.满额即还]
					'TotalShares' => '',//总共多少份
					'CastedShares' => '',//已投多少份
					'Province' => urlencode('广东'),//urlencode(mb_convert_encoding('广东',"UTF-8","GBK")),//省
					'City' => urlencode('深圳'),//urlencode(mb_convert_encoding('深圳',"UTF-8","GBK")),//市
					'UserName' => $val['username'],//urlencode(mb_convert_encoding($val['username'],"UTF-8","GBK")),//发标人
					'UserAvatarUrl' => '',//发标人头像
					'AmountUsedDesc' => '',//urlencode(mb_convert_encoding($val['use_type'],"UTF-8","GBK")),//用途
					'Revenue' => $revenue,//管理费
					'Subscribes' => $tmpUser,//投资人情报对象数据或LIST
				);
			}
			
			$cache->set_array($key, $data, 600);
		}
		
    	return json_encode($data);
	}
	
	function getFinishedBidInfo($token='', $date=''){
		if(md5(md5('wdzjunamewdzjpwd'))!=$token){
    		return "";
    	}
    	
    	include 'mymc.php';
		$cache = new Mymc();
		$key = 'api_bid_getFinishedBidInfo_'.$date;
		$data = $cache->get_array($key); 
		if ($data==false){
			$data = array();
			
			//include 'db.php'; //数据库连接
			
			$date = $date ? $date : date('Y-m-d');
			$date = date('Y-m-d',strtotime($date));
			$date_start = strtotime($date);
			$date_end = strtotime($date." 23:59:59");
			$sql = "select p1.*,p2.username,p3.name as use_type,p4.forder as limit_order from ych_borrow as p1 left join ych_user as p2 on p1.user_id=p2.user_id left join ych_linkage as p3 on p1.fuse=p3.id left join ych_borrow_repayment as p4 on p1.id=p4.borrow_id where p4.fstatus=1 and p4.repayment_yestime>={$date_start} and p4.repayment_yestime<={$date_end} and p1.ftype!=4";
			$list = self::db_fetch_arrays($sql,$conn);
			foreach($list as $key => $val){
				$type = 0;
				$RepaymentType = 3; //还款方式[1:到期还本息2.每月付息到期还本3.每月等额本息4.满额即还]
				if($val["ftype"]==5){
					$type = 1;//$tmptype[] = "秒";
					$RepaymentType = 4;
				}elseif($val["ftype"]==2){
					$type = 2;//$tmptype[] = "押";
				}elseif($val["ftype"]==3){
					$type = 3;//$tmptype[] = "净";
				}elseif($val["ftype"]==6){
					$type = 4;//$tmptype[] = "担";
				}elseif($val["ftype"]==4){
					$type = 6;//$tmptype[] = "流转";
					$RepaymentType = 2;
				}elseif($val["ftype"]==1){
					$type = 5;//$tmptype[] = "信";
				}
				
				$string = "/static/data/avatar/".$val['user_id']."_avatar_middle.jpg";
				$pic = "http://www.gsdai.com".$string;
				
				$status = 0;
				if($val['fstatus']==1){
					$status = 1;//招标中
				}elseif($val['fstatus'] == 3 && $val['repayment_account'] != $val['repayment_yesaccount']){
					$status = 2;//待还中
				}elseif($val['fstatus'] == 3 && $val['repayment_account'] == $val['repayment_yesaccount']){
					$status = 3;//已完成
				}elseif(($val['verify_time'] + $val['valid_time']*24*3688)<time()){//p1.verify_time+p1.valid_time*24*60*60<".time();
					$status = 4;//逾期中
				}
				
				//投标人查询
				$sql2 = "select p1.id,p2.username,p1.money,p1.account,p1.add_time from ych_borrow_tender as p1 left join ych_user as p2 on p1.user_id=p2.user_id where p1.borrow_id=".$val['id'];
				$tenderUser = self::db_fetch_arrays($sql2,$conn);
				$interestRate = number_format($val["apr"]/100, 4, '.','');//年利率
				$tmpUser = array();
				foreach($tenderUser as $k => $v){
					$tmpUser[] = array(
						'Id' => $v['id'],//ID
						'SubscribeUserId' => urlencode($v['username']),//投标人ID
						'Money' => $v['money'],//投标金额
						'Amount' => $v['account'],//有效金额
						'AddDate' => date('YmdHis',$v['add_time']),//投标时间
						'Status' => $status,//状态1:正在申购2:申购失败3:回收中3:逾期中4:投资完成
						'Type' => 1,
						'InterestRate' => $interestRate
					);
				}
				
				$data[] = array(
					'ProjectId' => $val['id'],//项目主键
					'Flag' => $val['time_limit'] == ($val['limit_order'] + 1) ? 1 : 0, //1全部还完0部分还完 
					'Title' => urlencode($val['title']),//借款标题
					'Amount' => $val['account'],//借款金额
					'Schedule' => number_format($val["account_yes"]/$val["account"], 4, '.',''),//完成进度
					'InterestRate' => $interestRate,//利率
					'TuandaiRate' => number_format($val['funds']/100, 4, '.',''),//奖励比例
					'Deadline' => $val["is_day"] == 1 ? $val["time_limit_day"] : $val["time_limit"],//借款期限
					'DeadLineUnit' => $val['is_day'] ? 1:0,//期限单位（1日0月）
					'Type'=> $type,//借款类型[1秒/2抵押/3净值/4担保/5信用/6流转标]
					'RepaymentType' => $RepaymentType,//还款方式[1:到期还本息2.每月付息到期还本3.每月等额本息4.满额即还]
					'TotalShares' => '',//总共多少份
					'CastedShares' => '',//已投多少份
					'Province' => urlencode('广东'),//省
					'City' => urlencode('深圳'),//市
					'UserName' => urlencode($val['username']),//发标人
					'UserAvatarUrl' => $pic,//发标人头像
					'AmountUsedDesc' => urlencode($val['use_type']),//用途
					'Subscribes' => $tmpUser,//投资人情报对象数据或LIST
				);
			}
			
			$sql = "SELECT p1.yjb_id,p1.time_limit_day,SUM(p1.account) AS account,p1.apr,p2.user_id,
				p2.title,p2.account AS old_account,p2.account_yes,p2.fstatus,p3.username 
				FROM ych_yjb_tender AS p1 
				LEFT JOIN ych_yjb_borrow AS p2 ON p1.yjb_id=p2.id 
				LEFT JOIN ych_user as p3 ON p2.user_id=p3.user_id 
				where p1.add_time>={$date_start} and p1.add_time<={$date_end} 
				GROUP BY p1.yjb_id,p1.time_limit_day";
			$list = self::db_fetch_arrays($sql,$conn);
			foreach ($list as $val){
				$sql2 = "select p1.id,p2.username,p1.money,p1.account,p1.add_time from ych_yjb_tender as p1 left join ych_user as p2 on p1.user_id=p2.user_id where p1.yjb_id={$val['yjb_id']} and p1.time_limit_day={$val['time_limit_day']}";
				$tenderUser = self::db_fetch_arrays($sql2,$conn);
				$interestRate = number_format($val["apr"]/100, 4, '.','');//年利率
				$tmpUser = array();
				foreach($tenderUser as $k => $v){
					$tmpUser[] = array(
						'Id' => $v['id'],//ID
						'SubscribeUserId' => urlencode($v['username']),//urlencode(mb_convert_encoding($v['username'],"UTF-8","GBK")),//投标人ID
						'Money' => $v['money'],//投标金额
						'Amount' => $v['account'],//有效金额
						'AddDate' => date('YmdHis',$v['add_time']),//投标时间
						'Status' => $val['fstatus']<4?3:5,//状态1:正在申购2:申购失败3:回收中4:逾期中5:投资完成
						'Type' => 1,
						'InterestRate' => $interestRate
					);
				}
				$revenue = round($val['account'] * 0.015,2);
				
				$data[] = array(
					'ProjectId' => $val['yjb_id'].'_'.$val['time_limit_day'].'_'.$today,//项目主键
					'Title' => urlencode($val['title']),//urlencode(mb_convert_encoding($val['name'],"UTF-8","GBK")),//借款标题
					'Amount' => $val['account'],//借款金额   流转标有区别
					'Schedule' => number_format($val["account_yes"]/$val["old_account"], 4, '.',''),//完成进度
					'InterestRate' => $interestRate,//利率
					'TuandaiRate' => number_format($val['funds']/100, 4, '.',''),//奖励比例
					'Deadline' => $val["time_limit_day"],//借款期限
					'DeadLineUnit' => 1,//期限单位（1日0月）
					'Type'=> 4,//借款类型[1秒/2抵押/3净值/4担保/5信用/6流转标]
					'RepaymentType' => 1,//还款方式[1:到期还本息2.每月付息到期还本3.每月等额本息4.满额即还]
					'TotalShares' => '',//总共多少份
					'CastedShares' => '',//已投多少份
					'Province' => urlencode('广东'),//urlencode(mb_convert_encoding('广东',"UTF-8","GBK")),//省
					'City' => urlencode('深圳'),//urlencode(mb_convert_encoding('深圳',"UTF-8","GBK")),//市
					'UserName' => $val['username'],//urlencode(mb_convert_encoding($val['username'],"UTF-8","GBK")),//发标人
					'UserAvatarUrl' => '',//发标人头像
					'AmountUsedDesc' => '',//urlencode(mb_convert_encoding($val['use_type'],"UTF-8","GBK")),//用途
					'Revenue' => $revenue,//管理费
					'Subscribes' => $tmpUser,//投资人情报对象数据或LIST
				);
			}
			$cache->set_array($key, $data, 600);
		}
		return json_encode($data);
	}
	
	function getPaiedLoanInfo($token='', $date=''){
		if(md5(md5('wdzjunamewdzjpwd'))!=$token){
    		return "";
    	}
    	include 'mymc.php';
		$cache = new Mymc();
		$key = 'api_bid_getPaiedLoanInfo_'.$date;
		$data = $cache->get_array($key); 
		if ($data==false){
			$data = array();
			$start = strtotime(date('Y-m-d',strtotime($date)));
	    	$end = $start + (3600*24);
	    	if(time()<$end){
	    		return "";
	    	}
	    	
	    	include 'db.php'; //数据连接
	    	
	    	$sql = "SELECT  p1.*,p2.is_cz,p2.name AS borrow_name,p2.is_fast,p2.time_limit,p3.username,p3.user_id,p3.phone,p3.area
				 FROM dw_borrow_repayment AS p1,dw_borrow AS p2 ,dw_user AS p3 
				 WHERE p1.borrow_id=p2.id AND p2.user_id=p3.user_id AND p2.status=3 AND p1.repayment_time>=$start AND 
				 p1.repayment_time<$end  ORDER BY id";
	    	$list = self::db_fetch_arrays($sql,$conn);
	    	$data = array();
	    	foreach($list as $val){
	    		$data[] = array(
	    			'Id' => $val['id'],
	    			'Status' => $val['status'] == 1 ? 0 : 1
	    		);
	    	}
	    	$cache->set_array($key, $data, 600);
		}
    	return json_encode($data);
	}
	
	function db_fetch_arrays($sql, $conn) 	{
		$result = mysql_query($sql, $conn);
		$i = 0;
		$_res = array();
		while(@$res = mysql_fetch_array($result,MYSQL_ASSOC)) {
			foreach ($res as $key => $value){
				$_res[$i][$key] = $value;//直接转义
			}
			$i++;
		}
		return $_res;
	}
	
	function db_fetch_array($sql,$conn) {
			$result = mysql_query($sql, $conn);
			$res = mysql_fetch_array($result,MYSQL_ASSOC);
			$_res = "";
			if (is_array($res)){
				foreach ($res as $key => $value){
					$_res[$key] =$value;
				}
			}
			$query = $_res;
			return $query;
	}
}
?>
